package net.maku.events.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.AllArgsConstructor;
import net.maku.events.entity.ActivityBriskWalkEntity;
import net.maku.events.enums.ActivityQuestionTypeEnum;
import net.maku.events.service.ActivityContestQuestionService;
import net.maku.events.vo.ActivityContestQuestionVO;
import net.maku.events.vo.ActivityTopVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.events.convert.ActivityKnowledgeContestConvert;
import net.maku.events.entity.ActivityKnowledgeContestEntity;
import net.maku.events.query.ActivityKnowledgeContestQuery;
import net.maku.events.vo.ActivityKnowledgeContestVO;
import net.maku.events.dao.ActivityKnowledgeContestDao;
import net.maku.events.service.ActivityKnowledgeContestService;
import net.maku.framework.security.user.SecurityUser;
import net.maku.system.service.SysOrgService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 知识竞赛活动
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@Service
@AllArgsConstructor
public class ActivityKnowledgeContestServiceImpl extends BaseServiceImpl<ActivityKnowledgeContestDao, ActivityKnowledgeContestEntity> implements ActivityKnowledgeContestService {

    private final ActivityContestQuestionService activityContestQuestionService;

    private final SysOrgService sysOrgService;

    @Override
    public PageResult<ActivityKnowledgeContestVO> page(ActivityKnowledgeContestQuery query) {
        IPage<ActivityKnowledgeContestEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(BeanUtil.copyToList(page.getRecords(), ActivityKnowledgeContestVO.class), page.getTotal());
    }

    private LambdaQueryWrapper<ActivityKnowledgeContestEntity> getWrapper(ActivityKnowledgeContestQuery query) {
        LambdaQueryWrapper<ActivityKnowledgeContestEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getActivityTitle()), ActivityKnowledgeContestEntity::getActivityTitle, query.getActivityTitle())
                .eq(ObjectUtils.isNotEmpty(query.getStatus()), ActivityKnowledgeContestEntity::getActivityStatus, query.getStatus())
                .eq(ObjectUtils.isNotEmpty(query.getActivityStatus()), ActivityKnowledgeContestEntity::getActivityStatus, query.getActivityStatus())
                .eq(ObjectUtils.isNotEmpty(query.getWhetherTop()), ActivityKnowledgeContestEntity::getWhetherTop, query.getWhetherTop())
                .orderByDesc(ActivityKnowledgeContestEntity::getId);
        dataScopeWrapper(wrapper);
        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ActivityKnowledgeContestVO vo) {
        sysOrgService.setActivityJoinOrgName(vo);
        ActivityKnowledgeContestEntity entity = ActivityKnowledgeContestConvert.INSTANCE.convert(vo);
        entity.setOrgId(SecurityUser.getUser().getOrgId());
        baseMapper.insert(entity);
        shareInsert(entity.getId(), vo.getActivityContestQuestionVOList());
    }

    /**
     * 共用插入操作
     *
     * @param id                            主键
     * @param activityContestQuestionVOList 题目信息
     */
    private void shareInsert(Long id, List<ActivityContestQuestionVO> activityContestQuestionVOList) {
        if (CollectionUtils.isNotEmpty(activityContestQuestionVOList)) {
            activityContestQuestionService.save(id, ActivityQuestionTypeEnum.KNOWLEDGE_CONTEST.getBusinessType(), activityContestQuestionVOList);
        }else{
            activityContestQuestionService.delete(ActivityQuestionTypeEnum.KNOWLEDGE_CONTEST.getBusinessType(), Arrays.asList(id));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ActivityKnowledgeContestVO vo) {
        sysOrgService.setActivityJoinOrgName(vo);
        ActivityKnowledgeContestEntity entity = ActivityKnowledgeContestConvert.INSTANCE.convert(vo);
        updateById(entity);
        shareInsert(entity.getId(), vo.getActivityContestQuestionVOList());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        ActivityKnowledgeContestEntity entity = new ActivityKnowledgeContestEntity();
        entity.setId(id);
        entity.setStatus(status);
        baseMapper.updateById(entity);
    }

    @Override
    public void whetherTop(ActivityTopVO activityTopVO) {
        ActivityKnowledgeContestEntity entity = new ActivityKnowledgeContestEntity();
        entity.setId(activityTopVO.getActivityId());
        entity.setWhetherTop(activityTopVO.getStatus());
        entity.setTopImg(activityTopVO.getTopImg());
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDataByIdList(List<Long> idList) {
        removeByIds(idList);
        activityContestQuestionService.delete(ActivityQuestionTypeEnum.KNOWLEDGE_CONTEST.getBusinessType(), idList);
    }

    @Override
    public ActivityKnowledgeContestVO getInfoById(Long id) {
        ActivityKnowledgeContestVO activityKnowledgeContestVO = BeanUtil.copyProperties(this.getById(id), ActivityKnowledgeContestVO.class);
        activityKnowledgeContestVO.setActivityContestQuestionVOList(activityContestQuestionService.findByBusinessIdList(Arrays.asList(id), ActivityQuestionTypeEnum.KNOWLEDGE_CONTEST.getBusinessType()));
        return activityKnowledgeContestVO;
    }

}